package com.lq.ecoldchain.model

import com.google.gson.Gson
import com.lq.ecoldchain.config.AppConfig
import com.lq.ecoldchain.entity.wechat.WxTokenBean
import com.lq.ecoldchain.entity.wechat.WxUserBean
import com.lq.ecoldchain.util.log
import com.lq.ecoldchain.util.toast
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import java.io.IOException
import java.util.concurrent.TimeUnit

/*
*2019/1/18 0018  12:09
*function by lq
*/
object WeChatModel {
    const val baseURL = "https://api.weixin.qq.com/sns/"

    fun loginAccessToken(code: String) {
        val httpLogger = HttpLoggingInterceptor()
        httpLogger.level = HttpLoggingInterceptor.Level.BODY
        val client = OkHttpClient.Builder()
                .addInterceptor(httpLogger)
                .connectTimeout(3, TimeUnit.SECONDS)
                .writeTimeout(3, TimeUnit.SECONDS)
                .readTimeout(3, TimeUnit.SECONDS)
                .build()
        val url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + AppConfig.WX_KEY_APP_ID + "&secret=" + AppConfig.WX_KEY_SECRET + "&code=" + code + "&grant_type=authorization_code"
        val request = Request.Builder().url(url).build()
        client.newCall(request).enqueue(object:Callback{
            override fun onResponse(call: Call, response: Response) {
                val tokenBean = Gson().fromJson<WxTokenBean>(response.body()!!.string(),WxTokenBean::class.java)
                log { "${tokenBean.refresh_token}" }
            }

            override fun onFailure(call: Call, e: IOException) {
                val message = ExceptionHandle.handleException(e)
                toast { message.message!! }
            }
        })
      /*  BaseNetCore.WeChatApiImpl.accessToken(AppConfig.WX_KEY_APP_ID,AppConfig.WX_KEY_SECRET,code,"authorization_code","1").compose(RxTransformer.switchSchedulers()).subscribe(object:Observer<WxTokenBean>{
            override fun onNext(t: WxTokenBean) {
                if(t.refresh_token!=null)
                    wxUserInfo(t.refresh_token!!,t.openid)
            }

            override fun onComplete() {

            }

            override fun onError(e: Throwable) {

            }

            override fun onSubscribe(d: Disposable) {

            }
        })*/
    }

    fun wxUserInfo(token:String,openId:String){
        BaseNetCore.WeChatApiImpl.userInfo(token,openId,"1").compose(RxTransformer.switchSchedulers()).subscribe(object:Observer<WxUserBean>{
            override fun onNext(t: WxUserBean) {
                log { t.toString() }
            }

            override fun onComplete() {

            }

            override fun onError(e: Throwable) {

            }

            override fun onSubscribe(d: Disposable) {

            }
        })
    }

    fun refreshToken(){
//        val request = Request.Builder()
//        https@ //api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
    }
}